package net.zxjava.gateway.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.stereotype.Service;

import net.zxjava.gateway.bean.Route;
import net.zxjava.gateway.dao.RouteDao;


@Service
public class RouteService {
	
	@Resource
    private RouteDao routeDao;
	
	public List<ZuulRoute> getZuulRoutes(String serverName){
		List<Route> routes = routeDao.getRoutes(serverName);
		List<ZuulRoute> zuulRoutes = new ArrayList<ZuulRoute>();
		  
		for(Route route:routes){
			ZuulRoute zuulRoute = new ZuulRoute();
			zuulRoute.setPath(route.getPath());
			zuulRoute.setId(route.getServiceId().toLowerCase());
			zuulRoute.setRetryable(route.getRetryable());
			zuulRoute.setUrl(route.getUrl());
			zuulRoute.setStripPrefix(route.getStripPrefix());
			zuulRoute.setServiceId(route.getServiceId());
			zuulRoute.setSensitiveHeaders(new HashSet<String>(Arrays.asList(route.getSensitiveHeaders().split(";"))));
			zuulRoutes.add(zuulRoute);
		}
		return zuulRoutes;
	}
}

